package com.wontube.smr.remote.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.GenreSelectBO;
import com.wontube.smr.bo.sites.MVSelectBO;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.MVChart;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MVChartItems;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.bo.MVSelectDao;
import com.wontube.smr.dao.bo.MovieSelectDao;
import com.wontube.smr.web.client.IMVWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * 音乐接口实现
 * 
 * @author hewc@wondershare.cn
 * 
 */
@NoLoginSupport
public final class MVWebHandler extends BasicHandler implements IMVWebHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> selectAllMusicGenres() {
		return GenreSelectBO.selectRemendedMusicGenres();
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Movie getMV(String mvId) {
		if (StringUtils.isNullOrEmpty(mvId)) {
			return null;
		}
		return MovieSelectDao.getMovie(mvId);		
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listRelated(String mvId, PagenateInfo pageInfo) {
		if (StringUtils.isNullOrEmpty(mvId)) {
			return new MovieItems();
		}
		return MVSelectBO.listRelated(mvId, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectMusicByArtist(String artist, PagenateInfo pageInfo) {
		return MovieSelectDao.selectVevoMusicsByArtist(artist, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listVevoMusics(PagenateInfo pageInfo) {
		return MVSelectBO.selectVevoMusics(pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listVevoMusics(QueryItemParams params,
			PagenateInfo pageInfo) {
		//修正genres
		params.setGenre(GenreSelectBO.joinMusicGenres(params.getGenre()));
		MovieItems items = MVSelectBO.selectVevoMusics(params, pageInfo);
		if(params.isNeedGenres()){
			items.setGenres(selectAllMusicGenres());
		}
		return items;
	}
	
	//MVChart
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChartItems listVevoTopArtists(PagenateInfo pageInfo) {
		return MVSelectBO.listVevoTopArtists(pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChart getMVChartByVideoId(String movieId) {
		List<MVChart> charts = MVSelectDao.getMVChartsByVideoId(movieId);
		if(charts.size() > 0){
			return charts.get(0);
		}
		return null;
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChartItems getVevoVideoCharts(String period, PagenateInfo pageInfo) {
		return MVSelectDao.selectMVCharts(MVChart.CATEGORY_VEVO_TOP_VIDEOS, period, pageInfo);		
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChartItems getVevoArtistCharts(String period, PagenateInfo pageInfo) {
		return MVSelectBO.selectVevoArtistCharts(period, pageInfo);
	}		

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, MVChartItems> getVevoChartsHome(PagenateInfo pageInfo) {
		Map<String, MVChartItems> maps = new HashMap<String, MVChartItems>();
		
		//daily top artist
		MVChartItems vevoArtists = MVSelectDao.selectMVCharts(MVChart.CATEGORY_VEVO_TOP_ARTISTS, 
				MVChart.UPDATE_PERIOD_DAILY, pageInfo);
		maps.put("vevoArtists", vevoArtists);		
		
		//daily top video
		MVChartItems vevoTops = MVSelectDao.selectMVCharts(MVChart.CATEGORY_VEVO_TOP_VIDEOS, 
				MVChart.UPDATE_PERIOD_DAILY, pageInfo);
		maps.put("vevoTops", vevoTops);
		return maps;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, Object> getMp3Home() {
		int pageSize = 50;
		/*
		 * vevoArtists -> MVChartItems
		 * vevoMusics -> MovieItems
		 */		
		Map<String, Object> maps = new HashMap<String, Object>();		
		maps.put("vevoMusics", MVSelectBO.selectVevoMusics(new PagenateInfo(1, pageSize)));
		maps.put("vevoArtists", MVSelectDao.selectMVCharts(MVChart.CATEGORY_VEVO_TOP_ARTISTS, 
				MVChart.UPDATE_PERIOD_DAILY, new PagenateInfo(1, pageSize/2)));
		
		return maps;
	}
	
	//=========i18n=============
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, MVChartItems> getYoutubeChartsHome() {
		int pageSize = 20;
		/*
		 * youtubeDailyItems -> MVChartItems
		 * youtubeWeeklyItems -> MVChartItems
		 * billboardItems -> MVChartItems
		 */
		Map<String, MVChartItems> results = new HashMap<String, MVChartItems>();		
		final PagenateInfo pageInfo = new PagenateInfo(1, pageSize);
		MVChartItems ytdailyItems = getYoutubeDailyCharts(pageInfo);
		results.put("youtubeDailyItems", ytdailyItems);
		
		MVChartItems ytweeklyItems = getYoutubeWeeklyCharts(pageInfo);
		results.put("youtubeWeeklyItems", ytweeklyItems);
		
		MVChartItems bbItems = MVSelectDao.selectMVCharts(MVChart.CATEGORY_BILLBOARD_TOP,
				MVChart.UPDATE_PERIOD_WEEKLY, pageInfo);
		results.put("billboardItems", bbItems);
		return results;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChartItems getYoutubeDailyCharts(PagenateInfo pageInfo) {
		return MVSelectDao.selectMVCharts(MVChart.CATEGORY_YOUTUBE_TOP,
				MVChart.UPDATE_PERIOD_DAILY, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MVChartItems getYoutubeWeeklyCharts(PagenateInfo pageInfo) {
		return MVSelectDao.selectMVCharts(MVChart.CATEGORY_YOUTUBE_TOP,
				MVChart.UPDATE_PERIOD_WEEKLY, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Movie getYoutubeMV(String mvId) {
		return MVSelectBO.getYoutubeMV(mvId);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listYoutubeMusics(QueryItemParams params, PagenateInfo pageInfo) {
		params.setGenre(GenreSelectBO.joinYoutubeI18nMusicGenres(params.getGenre()));
		
		MovieItems items = MovieSelectDao.selectMovieYoutbeMusic(params, pageInfo);
		
		if(params.isNeedGenres()){
			items.setGenres(getI18nMusicGenres());
		}
		return items;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> getI18nMusicGenres() {
		return GenreSelectBO.selectI18nMusicGenres();
	}	
}
